using System;
using AzureDiagMon.Common.Dtos;

namespace AzureDiagMon.Common.Services
{
    [Serializable]
    public class LogSyncServiceConfiguration : ICloneable
    {
        private readonly object syncRoot = new object();

        private StorageAccountDto storageAccount;

        public StorageAccountDto StorageAccount
        {
            get { return storageAccount; }
            set
            {
                lock (syncRoot)
                    storageAccount = value;
            }
        }

        private TimeSpan refreshInterval;

        public TimeSpan RefreshInterval
        {
            get { return refreshInterval; }
            set
            {
                lock (syncRoot)
                    refreshInterval = value;
            }
        }

        private string[] specificInstances;

        public string[] SpecificInstances
        {
            get { return specificInstances; }
            set
            {
                lock (syncRoot)
                    specificInstances = value;
            }
        }

        private DateFilter dateFilter;
        public DateFilter DateFilter
        {
            get { return dateFilter; }
            set
            {
                lock (syncRoot)
                {
                    dateFilter = (DateFilter)value.Clone();
                }
            }
        }

        public LogSyncServiceConfiguration()
        {

        }

        public LogSyncServiceConfiguration(StorageAccountDto storageAccount,
                                           TimeSpan refreshInterval,
                                           DateFilter dateFilter = null)
        {
            StorageAccount = storageAccount;
            RefreshInterval = refreshInterval;
        }

        public object Clone()
        {
            lock (syncRoot)
            {
                var copy = (LogSyncServiceConfiguration)MemberwiseClone();
                if (this.DateFilter != null)
                    copy.DateFilter = (DateFilter)this.DateFilter.Clone();
                return copy;
            }
        }

        public bool CalculatedStartAndEndDate(out DateTime startDate, out DateTime endDate)
        {
            bool isNowEnded;
            return DateFilter.CalculateStartAndEndDate(out startDate, out endDate, out isNowEnded);
        }

    }

}
